#include "ViewManager.h"
#include <iostream>
using namespace std;
using namespace sf;

/* Moves the camera on the target*/
void ViewManager::center_view()
{
	Vector2f target_coord=_target->getPosition();
	_camera.setCenter(target_coord);
	cout<<"Coord : "<<target_coord.x<<" : "<<target_coord.y<<endl;
}

/* Moves the camera if the target if to far from the center of the camera*/
void ViewManager::follow_target()
{
	Vector2f cam_pos=_camera.getCenter();
	Vector2f tar_pos=_target->getPosition();

	if (abs((int)cam_pos.x-(int)tar_pos.x)>_max_distance) //controls the X axis
	{
		if (cam_pos.x<tar_pos.x) //target is too far on the right
			cam_pos.x=(int)tar_pos.x-_max_distance;
		else
			cam_pos.x=(int)tar_pos.x+_max_distance;
	}
	if (abs((int)cam_pos.y-(int)tar_pos.y)>_max_distance) //controls the Y axis
	{
		if (cam_pos.y<tar_pos.y) //target is too far upside
			cam_pos.y=tar_pos.y-_max_distance;
		else
			cam_pos.y=tar_pos.y+_max_distance;
	}

	//Replace the camera if we reach the level's limits
	if(cam_pos.x<400)
		cam_pos.x=400;
	else if(cam_pos.x>6600)
		cam_pos.x=5600;

	if(cam_pos.y>900)
		cam_pos.y=900;

	_camera.setCenter(cam_pos);
}

void ViewManager::reset_view()
{
	_camera.setCenter(0,0);
}

View& ViewManager::getView()
{
	return _camera;
}